package com.bjsxt.shop.goods.pojo;

import java.io.Serializable;
import java.util.Date;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.bjsxt.shop.common.util.ConstatFinalUtil;
import com.bjsxt.shop.common.util.DateFormatUtil;
import com.bjsxt.shop.users.pojo.AAdmins;

/**
 * 商品的POJO
 * 
 * @author wangshSxt
 *
 */
public class AGoods implements Serializable
{
	private int id;
	private int adminsId;
	private int cateId;
	private int brandId;
	private String name;
	private String title;
	private String url;
	private double weight;
	private String content;
	private int saleNum;
	private int remainNum;
	private double marketPrice;
	private double price;
	private String listImgPath;
	private String imgs;
	private String spec;
	private byte status;
	private Date createTime;
	private Date updateTime;
	private Date pubTime;
	
	/* 字符串描述 */
	private String statusStr ; 
	
	/* 关联对象 */
	private AAdmins admins ; 
	private AGoodsCate goodsCate ; 
	private AGoodsBrand goodsBrand ; 
	private JSONArray specArr ;

	public int getId()
	{
		return id;
	}

	public void setId(int id)
	{
		this.id = id;
	}

	public int getAdminsId()
	{
		return adminsId;
	}

	public void setAdminsId(int adminsId)
	{
		this.adminsId = adminsId;
	}

	public int getCateId()
	{
		return cateId;
	}

	public void setCateId(int cateId)
	{
		this.cateId = cateId;
	}

	public int getBrandId()
	{
		return brandId;
	}

	public void setBrandId(int brandId)
	{
		this.brandId = brandId;
	}

	public String getName()
	{
		return name;
	}

	public void setName(String name)
	{
		this.name = name;
	}

	public String getTitle()
	{
		return title;
	}

	public void setTitle(String title)
	{
		this.title = title;
	}

	public String getUrl()
	{
		return url;
	}

	public void setUrl(String url)
	{
		this.url = url;
	}

	public double getWeight()
	{
		return weight;
	}

	public void setWeight(double weight)
	{
		this.weight = weight;
	}

	public String getContent()
	{
		return content;
	}

	public void setContent(String content)
	{
		this.content = content;
	}

	public int getSaleNum()
	{
		return saleNum;
	}

	public void setSaleNum(int saleNum)
	{
		this.saleNum = saleNum;
	}

	public int getRemainNum()
	{
		return remainNum;
	}

	public void setRemainNum(int remainNum)
	{
		this.remainNum = remainNum;
	}

	public double getMarketPrice()
	{
		return marketPrice;
	}

	public void setMarketPrice(double marketPrice)
	{
		this.marketPrice = marketPrice;
	}

	public double getPrice()
	{
		return price;
	}

	public void setPrice(double price)
	{
		this.price = price;
	}

	public String getListImgPath()
	{
		return listImgPath;
	}

	public void setListImgPath(String listImgPath)
	{
		this.listImgPath = listImgPath;
	}

	public String getImgs()
	{
		return imgs;
	}

	public void setImgs(String imgs)
	{
		this.imgs = imgs;
	}

	public String getSpec()
	{
		return spec;
	}

	public void setSpec(String spec)
	{
		this.spec = spec;
	}

	public byte getStatus()
	{
		return status;
	}

	public void setStatus(byte status)
	{
		this.status = status;
	}

	public Date getCreateTime()
	{
		return createTime;
	}

	public void setCreateTime(Date createTime)
	{
		this.createTime = createTime;
	}

	public Date getUpdateTime()
	{
		return updateTime;
	}

	public void setUpdateTime(Date updateTime)
	{
		this.updateTime = updateTime;
	}

	public Date getPubTime()
	{
		return pubTime;
	}

	public void setPubTime(Date pubTime)
	{
		this.pubTime = pubTime;
	}

	public AAdmins getAdmins()
	{
		return admins;
	}

	public void setAdmins(AAdmins admins)
	{
		this.admins = admins;
	}

	public AGoodsCate getGoodsCate()
	{
		return goodsCate;
	}

	public void setGoodsCate(AGoodsCate goodsCate)
	{
		this.goodsCate = goodsCate;
	}

	public AGoodsBrand getGoodsBrand()
	{
		return goodsBrand;
	}

	public void setGoodsBrand(AGoodsBrand goodsBrand)
	{
		this.goodsBrand = goodsBrand;
	}

	public String getStatusStr()
	{
		/*
		 * 状态:0:禁用;1:启用
		 */
		AGoodsEnum[] goodsEnums = AGoodsEnum.values();
		for (int i = 0; i < goodsEnums.length; i++)
		{
			AGoodsEnum goodsEnum = goodsEnums[i];
			if (goodsEnum.toString().startsWith("STATUS_"))
			{
				/* 如果循环的枚举==数据库存储的状态值,赋值枚举指定的字符串 */
				if (goodsEnum.getStatus() == this.status)
				{
					this.statusStr = goodsEnum.getInfo();
					break;
				}
			}
		}
		return statusStr;
	}

	public JSONArray getSpecArr()
	{
		try
		{
			this.specArr = (JSONArray) JSON.parse(this.spec);
		} catch (Exception e)
		{
			this.specArr = new JSONArray() ; 
		}
		return specArr;
	}

	public JSONObject toJSON()
	{
		DateFormatUtil dateFormatUtil = new DateFormatUtil() ; 
		
		JSONObject resultJSON = new JSONObject() ; 
		resultJSON.put("id", this.getId());
		resultJSON.put("adminsId", this.getAdminsId());
		resultJSON.put("cateId", this.getCateId());
		resultJSON.put("brandId", this.getBrandId());
		resultJSON.put("name", this.getName());
		resultJSON.put("title", this.getTitle());
		resultJSON.put("url", this.getUrl());
		resultJSON.put("weight", this.getWeight());
		resultJSON.put("content", this.getContent());
		resultJSON.put("saleNum", this.getSaleNum());
		resultJSON.put("remainNum", this.getRemainNum());
		resultJSON.put("marketPrice", this.getMarketPrice());
		resultJSON.put("price", this.getPrice());
		resultJSON.put("status", this.getStatus());
		resultJSON.put("statusStr", this.getStatusStr());
		resultJSON.put("createTime", dateFormatUtil.dateTimeStr(this.getCreateTime()));
		resultJSON.put("updateTime", dateFormatUtil.dateTimeStr(this.getUpdateTime()));
		resultJSON.put("pubTime", dateFormatUtil.dateTimeStr(this.getPubTime()));
		
		if(admins != null)
		{
			resultJSON.put("adminsName", this.admins.getTrueName());
			resultJSON.put("admins", admins.toJSON());
		}
		
		if(goodsCate != null)
		{
			resultJSON.put("cateName", this.goodsCate.getName());
			/*resultJSON.put("cate", admins.toJSON());*/
		}
		if(goodsBrand != null)
		{
			resultJSON.put("brandName", this.goodsBrand.getName());
			/*resultJSON.put("cate", admins.toJSON());*/
		}
		return resultJSON;
	}

	public AGoods parseJSON(JSONObject sourceJSON)
	{
		this.setId(Integer.valueOf(sourceJSON.get("id") + ""));
		this.setName(sourceJSON.get("name") + "");
		this.setPrice(Double.valueOf(sourceJSON.get("price") + ""));
		return this ;  
	}

}
